package com.wuqi.dev.mvc.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wuqi.dev.mvc.R;
import com.wuqi.dev.mvc.entity.GankEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Risky57 on 2017/4/19.
 */

public class GankAdapter extends BaseAdapter {

    // 适配器里面的数据, 只放集合类型的
    private List<GankEntity.ResultsBean> results;
    private LayoutInflater mInflater;
    private Context mContext;

    public GankAdapter (Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        results = new ArrayList<>();
    }

    public void addAll(List<GankEntity.ResultsBean> results){
        this.results.clear();
        this.results.addAll(results);
        notifyDataSetChanged();
    }

    @Override
    public int getCount () {
        return results.size();
    }

    @Override
    public Object getItem (int position) {
        return results.get(position);
    }

    @Override
    public long getItemId (int position) {
        return 0;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_gank_image, parent, false);
        }

        ImageView imageView = (ImageView) convertView;
        Glide.with(mContext).load(results.get(position).getUrl()).into(imageView);

        return convertView;
    }
}
